package com.ra.dao;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

public abstract class AbstractDAO implements BaseDAO {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@SuppressWarnings("unchecked")
	public <E> E save(E e) {
		Session currentSession = sessionFactory.getCurrentSession();
		e = (E) currentSession.save(e);
		return e;
	}

	public <E> E update(E e) {
		Session currentSession = sessionFactory.getCurrentSession();
		currentSession.update(e);
		return e;
	}

	public <E> void delete(E e) {
		Session currentSession = sessionFactory.getCurrentSession();
		currentSession.delete(e);
	}

	@SuppressWarnings("unchecked")
	public <E> E getById(Class<E> cls, int id) {
		Session currentSession = sessionFactory.getCurrentSession();
		E e = (E) currentSession.load(cls, id);
		return e;
	}

}
